package com.ftd.DBConnection;

import java.sql.*;

/**
 * Created by apple on 16/8/4.
 */
public class DBConnection {
    public static Connection getConnection(){
        String url = "jdbc:mysql://localhost:3306/HiBook";
        Connection connection = null;
        String name = "root";
        String password = "1";
        try{

            Class.forName("com.mysql.jdbc.Driver");

            connection = DriverManager.getConnection(url,name,password);


        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }

        return connection;

    }

    public static void insert(String phonenumber){

        Connection connection = getConnection();

        String sql = "INSERT INTO user(phonenumber) VALUES(?)";

        PreparedStatement preparedStatement = null;


        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,phonenumber);
            preparedStatement.execute();
            preparedStatement.close();
            connection.close();

        } catch (SQLException e){
            e.printStackTrace();
        }

    }
    public static void main(String args[]){
        insert("15564140625");
    }

}
